/**
 * 时间格式化
 * data 时间值
 * type 格式化类型，暂支持四种格式(1,2,3,4)默认为1
 * 格式1 yyyy-mm-dd HH:mm:ss
 * 格式2 yyyy-mm-dd
 * 格式3 yyyy-mm-dd 00:00:00
 * 格式4 yyyy-mm-dd 23:59:59
 */
function timeFormat(data, type = 1) {
  let timeData = "";
  if (data) {
    data += "";
    timeData = new Date(data.replace(/-/g, "/"));
    const y = timeData.getFullYear();
    const m = (timeData.getMonth() + 1).toString().padStart(2, "0");
    const d = (timeData.getDate() + 0).toString().padStart(2, "0");
    const h = (timeData.getHours() + 0).toString().padStart(2, "0");
    const min = (timeData.getMinutes() + 0).toString().padStart(2, "0");
    const s = (timeData.getSeconds() + 0).toString().padStart(2, "0");
    switch (type) {
      case 1:
        timeData = `${y}-${m}-${d} ${h}:${min}:${s}`;
        break;
      case 2:
        timeData = `${y}-${m}-${d}`;
        break;
      case 3:
        timeData = `${y}-${m}-${d} 00:00:00`;
        break;
      case 4:
        timeData = `${y}-${m}-${d} 23:59:59`;
        break;
      default:
        break;
    }
  }
  return timeData;
}

// 根据身份证号获取生日
function getBirthdayFromIdCard(idCard) {
  if (!idCard) {
    return "";
  }
  var birthday = "";
  if (idCard !== null && idCard !== "") {
    if (idCard.length === 15) {
      birthday = "19" + idCard.substr(6, 6);
    } else if (idCard.length === 18) {
      birthday = idCard.substr(6, 8);
    }
    birthday = birthday.replace(/(.{4})(.{2})/, "$1-$2-");
  }
  return birthday;
}
function getTheadShow() {
  // 动态表格表头数据处理初始值 (辅助控制动态表格的按钮状态)
  return [
    {
      prop: "oneStatus",
      label: "1月",
      month: "one",
      show: false,
      desc: "oneDesc",
      btnShow: "oneShow",
      isBack: "oneIsBack",
      limitDay: "oneLimitDay"
    },
    {
      prop: "twoStatus",
      label: "2月",
      month: "two",
      show: false,
      desc: "twoDesc",
      btnShow: "twoShow",
      isBack: "twoIsBack",
      limitDay: "twoLimitDay"
    },
    {
      prop: "threeStatus",
      label: "3月",
      month: "three",
      show: false,
      desc: "threeDesc",
      btnShow: "threeShow",
      isBack: "threeIsBack",
      limitDay: "threeLimitDay"
    },
    {
      prop: "fourStatus",
      label: "4月",
      month: "four",
      show: false,
      desc: "fourDesc",
      btnShow: "fourShow",
      isBack: "fourIsBack",
      limitDay: "fourLimitDay"
    },
    {
      prop: "fiveStatus",
      month: "five",
      label: "5月",
      show: false,
      desc: "fiveDesc",
      btnShow: "fiveShow",
      isBack: "fiveIsBack",
      limitDay: "fiveLimitDay"
    },
    {
      prop: "sixStatus",
      month: "six",
      label: "6月",
      show: false,
      desc: "sixDesc",
      btnShow: "sixShow",
      isBack: "sixIsBack",
      limitDay: "sixLimitDay"
    },
    {
      prop: "sevenStatus",
      month: "seven",
      label: "7月",
      show: false,
      desc: "sevenDesc",
      btnShow: "sevenShow",
      isBack: "sevenIsBack",
      limitDay: "sevenLimitDay"
    },
    {
      prop: "eightStatus",
      month: "eight",
      label: "8月",
      show: false,
      desc: "eightDesc",
      btnShow: "eightShow",
      isBack: "eightIsBack",
      limitDay: "eightLimitDay"
    },
    {
      prop: "nineStatus",
      month: "nine",
      label: "9月",
      show: false,
      desc: "nineDesc",
      btnShow: "nineShow",
      isBack: "nineIsBack",
      limitDay: "nineLimitDay"
    },
    {
      prop: "tenStatus",
      month: "ten",
      label: "10月",
      show: false,
      desc: "tenDesc",
      btnShow: "tenShow",
      isBack: "tenIsBack",
      limitDay: "tenLimitDay"
    },
    {
      prop: "elevenStatus",
      month: "eleven",
      label: "11月",
      show: false,
      desc: "elevenDesc",
      btnShow: "elevenShow",
      isBack: "elevenIsBack",
      limitDay: "elevenLimitDay"
    },
    {
      prop: "twelveStatus",
      month: "twelve",
      label: "12月",
      show: false,
      desc: "twelveDesc",
      btnShow: "twelveShow",
      isBack: "twelveIsBack",
      limitDay: "twelveLimitDay"
    }
  ];
}
// 深拷贝
function deepClone(target) {
  // 定义一个变量
  let result;
  // 如果当前需要深拷贝的是一个对象的话
  if (typeof target === "object") {
    // 如果是一个数组的话
    if (Array.isArray(target)) {
      result = []; // 将result赋值为一个数组，并且执行遍历
      for (let i in target) {
        // 递归克隆数组中的每一项
        result.push(deepClone(target[i]));
      }
      // 判断如果当前的值是null的话；直接赋值为null
    } else if (target === null) {
      result = null;
      // 判断如果当前的值是一个RegExp对象的话，直接赋值
    } else if (target.constructor === RegExp) {
      result = target;
    } else {
      // 否则是普通对象，直接for in循环，递归赋值对象的所有值
      result = {};
      for (let i in target) {
        result[i] = deepClone(target[i]);
      }
    }
    // 如果不是对象的话，就是基本数据类型，那么直接赋值
  } else {
    result = target;
  }
  // 返回最终结果
  return result;
}
export { timeFormat, getBirthdayFromIdCard, getTheadShow, deepClone };
